//
//  ZMVisitorView.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/23.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import SnapKit
class ZMVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //懒加载圈圈 visitordiscover_feed_image_smallicon
    lazy var cycleimageView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //懒加载遮罩
    lazy var maskImageview :UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //懒加载大房子
    lazy var HouseIconView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //懒加载消息提示框
    lazy var messageLabel = UILabel(text: "xiaoxi", fontSize: 13, textColor: UIColor.darkGray)
    
    //懒加载登录按钮
    lazy var loginButton : UIButton = {
       
        let btn = UIButton()
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white").stretchableImage(withLeftCapWidth: 5, topCapHeight: 5), for: UIControlState.normal)
        
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        //取消点击时候的闪烁
        btn.adjustsImageWhenHighlighted = false
        
        //登录按钮单击事件
        btn.addTarget(self, action: #selector(loginBtnClick(sender:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
    //懒加载注册按钮
    lazy var registButton : UIButton = {
       
        let btn = UIButton()
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white").stretchableImage(withLeftCapWidth: 5, topCapHeight: 5), for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(registbtnClick(sender:)), for: UIControlEvents.touchUpInside)
        return btn
        
    }()
    
    //按钮单击回调函数
    var visitorViewBtnClickCallBack:((UIButton)->())?
    
    
    //登录按钮单击事件
    func loginBtnClick(sender: UIButton){
        visitorViewBtnClickCallBack?(sender)
       
    }
    //注册按钮单击事件
    func registbtnClick(sender: UIButton){
        visitorViewBtnClickCallBack?(sender)
        
    }
    
    
    //初始化ui
   func setupUI(){
    
    //设置view的背景色
    self.backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
    
    
    //创建圈圈
    addSubview(cycleimageView)
    cycleimageView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
    }
    //遮罩
    addSubview(maskImageview)
    maskImageview.snp.makeConstraints { (make) in
        make.center.equalTo(cycleimageView)
    }
    //大房子
    addSubview(HouseIconView)
    HouseIconView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
    }
    //设置消息提示
    addSubview(messageLabel)
    messageLabel.snp.makeConstraints { (make) in
        make.centerX.equalToSuperview()
        make.top.equalTo(cycleimageView.snp.bottom).offset(10)
        make.width.equalTo(220)
    }
    //登录按键
    addSubview(loginButton)
    loginButton.snp.makeConstraints { (make) in
        make.top.equalTo(messageLabel.snp.bottom).offset(10)
        make.leading.equalTo(messageLabel)
        make.width.equalTo(80)
        make.height.equalTo(35)
    }
    //注册按键
    addSubview(registButton)
    registButton.snp.makeConstraints { (make) in
        make.top.equalTo(loginButton)
        make.trailing.equalTo(messageLabel.snp.trailing)
        make.height.equalTo(35)
        make.width.equalTo(80)
        
    }
   
    
   // startAnimation()
    
    }
    //设置圈圈动画
    func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2*Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        //添加动画
        
        cycleimageView.layer.add(animation, forKey: nil)
        
    }
    
    //外部接口函数,可以设置显示的信息,和图片
    func updateVisitorInfo(message:String,iconImageName:String,isHomeView:Bool){
        
        if !isHomeView {
            messageLabel.text = message
            HouseIconView.image = UIImage(named: iconImageName)
            //隐藏动画
            cycleimageView.isHidden = true
            
            return
        }
        //是在主页就需要显示动画
        startAnimation()
        
    }
    
    
    
}
